package edu.gznu.ics.fast.pulsar.service.prestoacc.picService.impl;

import edu.gznu.ics.fast.pulsar.dao.prestoacc.pic.CompareQueryMapper;
import edu.gznu.ics.fast.pulsar.dao.prestoacc.pic.PicQueryViewMapper;
import edu.gznu.ics.fast.pulsar.model.vo.web.CompareQueryResultVO;
import edu.gznu.ics.fast.pulsar.model.vo.web.CompareQueryVO;
import edu.gznu.ics.fast.pulsar.model.vo.web.PicQueryResultVO;
import edu.gznu.ics.fast.pulsar.service.prestoacc.picService.CompareQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by gpu on 17-9-12.
 */
@Service("compareQueryService")
public class CompareQueryServiceImply implements CompareQueryService {
    @Autowired
    CompareQueryMapper compareQueryMapper;

    @Autowired
    PicQueryViewMapper picQueryViewMapper;

    @Override
    public List<CompareQueryVO> queryByPicAndFits(CompareQueryVO compareQueryVO){
        System.out.println("service:"+compareQueryVO.getDm());
        List<CompareQueryVO> list =compareQueryMapper.queryByPicAndFits(compareQueryVO);
        return list;
    }
    @Override
    public List<CompareQueryResultVO> queryByRank(CompareQueryVO compareQueryVO,CompareQueryVO obj){
        List<CompareQueryResultVO> list =new ArrayList<>();
        List<CompareQueryVO> list2 =compareQueryMapper.queryByRank(compareQueryVO);
        System.out.println(compareQueryVO.getDm()+"/*/*/"+compareQueryVO.getPtopo()+"/*/*/"+compareQueryVO.getDec2000());
        System.out.println(list2.size());
        System.out.println(obj.getDec2000()+"~~~"+obj.getRa2000());
        for(int i=0;i<list2.size();i++){
            System.out.println(list2.get(i).getPtopo());

            list2.get(i).setPtopo(list2.get(i).getPtopo().multiply(new BigDecimal(1000)));

            System.out.println(list2.get(i).getPtopo());
            CompareQueryResultVO compareQueryResultVO=new CompareQueryResultVO();
//            compareQueryResultVO.setOrder(i+1);
            compareQueryResultVO.setPuslarName(list2.get(i).getPuslarName());
            compareQueryResultVO.setPtopo(list2.get(i).getPtopo());
            compareQueryResultVO.setDm(list2.get(i).getDm());
            compareQueryResultVO.setRa2000(list2.get(i).getRa2000());
            compareQueryResultVO.setDec2000(list2.get(i).getDec2000());

            Double ra_dec_d=Math.sqrt(Math.pow(list2.get(i).getDec2000()-obj.getDec2000(),2)+Math.pow(list2.get(i).getRa2000()-obj.getRa2000(),2));
            compareQueryResultVO.setRa_dec_d(ra_dec_d);
            BigDecimal Dm=(list2.get(i).getDm().subtract(obj.getDm())).abs();
            compareQueryResultVO.setDm_d(Dm);
            //BigDecimal p0_d=list2.get(i).getPtopo().subtract(obj.getPtopo());
            //compareQueryResultVO.setP0_d(p0_d);
            int hRank=obj.gethRank();
            //double h0_d=0;
            int h0_d1=0;
            int h0_d2=0;
            BigDecimal p0_abs_d=new BigDecimal(0);
            BigDecimal h0_d_pre=new BigDecimal(0);

            for(int k=1;k<=hRank;k++){
                for(int num=1;num<=hRank;num++){
                    BigDecimal bigDecimal;
                    BigDecimal p0_abs_dTemp;
                    BigDecimal pktemp=list2.get(i).getPtopo();
                    BigDecimal p0temp=obj.getPtopo();
                    if(pktemp.compareTo(new BigDecimal(0))!=0){
                        p0_abs_dTemp=(pktemp.subtract((p0temp.multiply(new BigDecimal(k))).divide(new BigDecimal(num),10, BigDecimal.ROUND_HALF_DOWN))).abs();
                        bigDecimal=p0_abs_dTemp.divide(pktemp, 10, BigDecimal.ROUND_HALF_DOWN);
                        if(bigDecimal.compareTo(new BigDecimal(0.001).multiply(obj.getpRank()))<1){

                            //h0_d=(float)k/num;
                            h0_d1=k;
                            h0_d2=num;
                            p0_abs_d=p0_abs_dTemp;
                            //System.out.println(k+"!!"+num+""+h0_d);
                            h0_d_pre=bigDecimal.multiply(new BigDecimal(100));
                            break;
                        }
                    }
                }
            }


            /*for(int k=1;k<=hRank;k++){
                BigDecimal bigDecimal;
                if(list2.get(i).getPtopo().compareTo(new BigDecimal(0))!=0){
                    bigDecimal=(list2.get(i).getPtopo().subtract(obj.getPtopo().multiply(new BigDecimal(k)))).abs().divide(list2.get(i).getPtopo(), 10, BigDecimal.ROUND_HALF_DOWN);

                    if(bigDecimal.compareTo(new BigDecimal(0.001))<1){
                        h0_d=k;
                        h0_d_pre=bigDecimal;
                        break;
                    }
                }
                if(obj.getPtopo().compareTo(new BigDecimal(0))!=0){
                    bigDecimal=(obj.getPtopo().subtract(list2.get(i).getPtopo().multiply(new BigDecimal(k)))).abs().divide(obj.getPtopo(), 10, BigDecimal.ROUND_HALF_DOWN);
                    if(bigDecimal.compareTo(new BigDecimal(0.001))<1){
                        h0_d=1/k;
                        h0_d_pre=bigDecimal;
                        break;
                    }
                }

            }*/

            //System.out.println("!!"+h0_d);
            compareQueryResultVO.setH0(h0_d1+"/"+h0_d2);
            System.out.println("!!"+compareQueryResultVO.getH0());
            compareQueryResultVO.setP0_d(h0_d_pre);
            compareQueryResultVO.setP0_percent(p0_abs_d.setScale(4,BigDecimal.ROUND_HALF_DOWN)+"("+h0_d_pre.setScale(4,BigDecimal.ROUND_HALF_DOWN)+"%)");
            list.add(compareQueryResultVO);


        }
        List<CompareQueryResultVO> listResult=new ArrayList<>();
        for(int i=0,j=0;i<list.size();i++){
            if (list.get(i).getP0_d().compareTo(new BigDecimal(0))!=0){
                j++;
                list.get(i).setOrder(j);
                listResult.add(list.get(i));
            }

        }
        System.out.println("/*"+listResult.size()+"******/");
        return listResult;
    }

    @Override
    public void  matchUpdate(PicQueryResultVO picQueryResultVO){
        picQueryViewMapper.matchUpdate(picQueryResultVO);

    }
    @Override
    public void  notesUpdate(PicQueryResultVO picQueryResultVO){
        picQueryViewMapper.notesUpdate(picQueryResultVO);

    }
}
